package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.commonutils.results;
import com.entity.pojo.Categories;
import com.entity.pojo.CategoriesRelation;
import com.entity.pojo.CoursesRelation;
import com.service.CategoriesRelationService;
import com.service.CategoriesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 目录关系表 前端控制器
 * </p>
 *
 * @author chenMo
 * @since 2022-04-11
 */

@RestController
@RequestMapping("/categoriesRelation")
@CrossOrigin
public class CategoriesRelationController {
    @Resource
    private CategoriesRelationService categoriesRelationService;

    @Resource
    private CategoriesService categoriesService;

    @ApiOperation("根据目录id查询关联目录列表")
    @GetMapping("/getNextCategoryList/{categoryId}")
    public results getNextCourseList(@PathVariable Integer categoryId) {
        List<CategoriesRelation> categoryRelationList = categoriesRelationService.list(
                new QueryWrapper<CategoriesRelation>().eq("categoryId", categoryId));
        List<Integer> NextCategoryIds = categoryRelationList.stream()
                .map(CategoriesRelation::getNextCategoryId)
                .collect(Collectors.toList());
        // 查询关联等级，用来动态渲染关联课程标签的颜色
        List<Integer> relationLevelList = categoryRelationList.stream()
                .map(CategoriesRelation::getRelationLevel)
                .collect(Collectors.toList());
        if (!NextCategoryIds.isEmpty()) {
            Collection<Categories> nextCategoryList = categoriesService.listByIds(NextCategoryIds);
            return results.ok().data("nextCategoryList", nextCategoryList).data("relationLevelList",relationLevelList);
        }
        return results.ok().data("nextCategoryList", null).data("relationLevelList",null);
    }

    @ApiOperation("根据目录id查询可以添加的关联目录列表")
    @GetMapping("/getCanBeAddedNextCategoryList/{currentCategoryId}/{courseId}")
    public results getCanBeAddedNextCategoryList(@PathVariable Integer currentCategoryId,@PathVariable Integer courseId) {
        List<Categories> canBeAddedNextCategoryList = categoriesRelationService.getCanBeAddedNextCategoryList(currentCategoryId,courseId);
        return results.ok().data("canBeAddedNextCategoryList", canBeAddedNextCategoryList);
    }

    @ApiOperation("增加新的关联目录")
    @PostMapping("addCategoryRelation")
    public results addCategoryRelation(@RequestBody CategoriesRelation categoryRelation) {
        if (categoryRelation.getNextCategoryId() == null){
            return results.error("关联目录不能为空");
        }
        if (categoryRelation.getRelationLevel() == null){
            return results.error("关联等级不能为空");
        }
        boolean saveFlag = categoriesRelationService.addCategoryRelation(categoryRelation);
        if (saveFlag) {
            return results.ok();
        } else {
            return results.error("增加关联目录信息失败");
        }
    }

    @ApiOperation("删除关联目录")
    @DeleteMapping("deleteCategoryRelation/{categoryId}/{nextCategoryId}")
    public results deleteCategoryRelation(@PathVariable Integer categoryId, @PathVariable Integer nextCategoryId) {
        boolean deleteFlag = categoriesRelationService.deleteCategoryRelation(categoryId, nextCategoryId);
        if (deleteFlag) {
            return results.ok();
        } else {
            return results.error("删除关联课程失败");
        }
    }
}

